<?php

namespace App\Services\Admin;

use App\Models\DiscountMeal as DiscountMealModel;
use App\Models\DiscountInfo as DiscountInfoModel;
use Illuminate\Support\Facades\DB;

class Binding extends Base
{
    public static function lists()
    {
        $input = request()->all();

        $where = [];

        if(!empty($input['sear_value'])){
            $where[] = ['class_title', 'like', '%'.$input['sear_value'].'%'];
        }

        if(!empty($input['is_enable'])){
            $where[] = ['is_enable',$input['is_enable']];
        }

        return (new DiscountMealModel)->where($where)->orderBy('id', 'desc')->paginate(15);
    }

    public static function info($id)
    {
        return (new DiscountMealModel)->where('id', $id)->first();
    }

    public static function discount_info($id)
    {
        return (new DiscountInfoModel())->where('discount_id', $id);
    }

    public static function add($param = [])
    {
        //开启事务
        DB::beginTransaction();
        try{
            $discount_new = (new DiscountMealModel);
            $discount_new->discount_name = $param['discount_name'];
            if(isset($param['cove']))$discount_new->cove = $param['cove'];
            $discount_new->original_price = $param['original_price'];
            $discount_new->is_enable = $param['is_enable'];
            $discount_new->meal_day = $param['meal_day'];
            $discount_new->discount_price = $param['discount_price'];
            $discount_new->points_attention = $param['points_attention'];
            $discount_new->save();
            if($discount_new->id){

                if(empty($param['class_base_info_id']))throw new \Exception('请选择课程安排!');

                $class_base_info_ids = explode(',', $param['class_base_info_id']);

                $class_base_info_insert = [];

                foreach ($class_base_info_ids as $v){
                    $input['discount_id'] = $discount_new->id;
                    $input['class_info_id'] = $v;
                    $input['created_at'] = date('Y-m-d h:i:s',time());
                    $input['updated_at'] = date('Y-m-d h:i:s',time());
                    $class_base_info_insert[] = $input;
                }

                (new DiscountInfoModel())->insert($class_base_info_insert);

                //更新字段
                $reservation_lists = Reservation::lists()->whereIn('id', $class_base_info_ids)->get()->toArray();

                $string_class_name = implode(",",array_column(array_column($reservation_lists,'class_base'),'class_name'));

                $discount_new->where('id',$discount_new->id)->update(['class_title'=>$string_class_name]);

            }else{

                throw new \Exception('添加失败!');

            }

            DB::commit();

        }catch (\Exception $e) {

            //接收异常处理并回滚
            DB::rollBack();

            return ['bool'=>false, 'message'=>$e->getMessage()];

        }

        return ['bool'=>true, 'message'=>''];

    }

    public static function edit($id , $param=[])
    {
        //开启事务
        DB::beginTransaction();
        try{

            if($id){

                if(empty($param['class_base_info_id']))throw new \Exception('请选择课程安排!');

                //清空了再入库
                (new DiscountInfoModel())->where('discount_id',$id)->delete();

                $class_base_info_ids = explode(',', $param['class_base_info_id']);

                unset($param['class_base_info_id']);

                $class_base_info_insert = [];

                foreach ($class_base_info_ids as $v){
                    $input['discount_id'] = $id;
                    $input['class_info_id'] = $v;
                    $input['created_at'] = date('Y-m-d h:i:s',time());
                    $input['updated_at'] = date('Y-m-d h:i:s',time());
                    $class_base_info_insert[] = $input;
                }

                (new DiscountInfoModel())->insert($class_base_info_insert);

                //更新字段
                $reservation_lists = Reservation::lists()->whereIn('id', $class_base_info_ids)->get()->toArray();

                $string_class_name = implode(",",array_column(array_column($reservation_lists,'class_base'),'class_name'));

                $discount_new = (new DiscountMealModel);
                $param['class_title'] = $string_class_name;
                $discount_new->where('id',$id)->update($param);

            }else{

                throw new \Exception('添加失败!');

            }

            DB::commit();

        }catch (\Exception $e) {

            //接收异常处理并回滚
            DB::rollBack();

            return ['bool'=>false, 'message'=>$e->getMessage()];

        }

        return ['bool'=>true, 'message'=>''];
    }

    public static function del($id)
    {
        (new DiscountMealModel)->where('id', $id)->delete();
        (new DiscountInfoModel())->where('discount_id', $id)->delete();
    }
}
